import {createStore} from 'vuex'
import {PropType} from 'vue'
import {ColumnProps,testColumnData,testPostsData} from '@/testDate/ColumnDate/index'

interface UserProps {
    isLogin: boolean,
    name?:string,
    id?:number,
}

export interface PostProps {
    id: number,
    title: string,
    content: string,
    imgurl?: string,
    createdAt: string, // 时间
    columnId: number
}

// export interface ResponseType<P = {}> {
//     code: number;
//     msg: string;
//     data: P;
//   }


export interface GlobalDataProps {
    columns: ColumnProps[],
    posts: PostProps[],
    user: UserProps,
    enAbleCreate?: boolean,
    currentId?: number 
}
const store = createStore <GlobalDataProps>({
    state: {
        columns: testColumnData,
        posts: testPostsData,
        user: {isLogin: false},
        enAbleCreate: true,
        currentId: 0

    },
    mutations: {
        login(state) {
            console.log('登录：',state);
            
            state.user = {...state.user,isLogin:true,name: 'hmgdec'}
        },
        loginOut(state) {
            state.user = {...state.user,isLogin:false}
        },
        setUnAbleCreate(state,data) {
            state.enAbleCreate = data
        },
        // 设置当前的columnId
        setCurrentId(state,data) {
            state.currentId = data
        },

        // 设置当前的posts数据 :PropType<PostProps>
        setPostsData(state,data) {
            state.posts = [data,...state.posts]
        }

    },
    // 状态管理工具的 计算属性，方便在多个页面使用
    getters: {
        getColumnById:(state) => (id:number) => {
          return state.columns.find(c => c.id == id)  
        },

        getPostListById: (state) => (id: number) => {
            return state.posts.filter(c => c.columnId == id)
        },

        // 获取当前的Column的数据
        getCurrentColumn: (state)=>  ()=> {
            const val = state.columns.find(c => c.id == state.currentId)
            if (val) {
                return JSON.parse(JSON.stringify(val))
            }
            
        }
    }
})

export default store